package com.capgemini.fs.app.ekms.persistence.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;

/**
 * 
 * @author nico
 * 
 */
public class DatabaseHelper {
	// private static Logger log = Logger.getLogger(DatabaseHelper.class);
	private static Configuration	configuration;
	private static SessionFactory	sessionFactory;
	private static Session			session;
	private static Properties		properties;
	private static final String		CONFIG_FILE_LOCATION		= "hibernate.cfg.xml";
	private static final String		CONFIG_PROPERTIES_LOCATION	= "hibernate.properties";

	private DatabaseHelper() {

	}

	/**
	 * Get Hibernate Configuration<br/>
	 * 
	 * @return Configuration
	 * @throws IOException
	 * @throws Exception
	 */
	public static Configuration getConfiguration() throws IOException {
		if (configuration == null) {
			Configuration cfg = new Configuration();
			configuration = cfg.configure(CONFIG_FILE_LOCATION);
			configuration.setProperties(getProperties());
		}

		return configuration;
	}

	/**
	 * Get Hibernate Session<br/>
	 * 
	 * @return Session
	 * @throws IOException
	 * @throws HibernateException
	 * @throws Exception
	 * @throws HibernateException
	 */
	public static Session getSession() throws HibernateException, IOException {
		if (session == null) {
			SessionFactory factory = getSessionFactory();
			session = factory.openSession();
		}
		return session;
	}

	/**
	 * Create database structure<br/>
	 * 
	 * @throws HibernateException
	 * @throws IOException
	 */
	public static void createSchema() throws HibernateException, IOException {
		SchemaExport exportor = new SchemaExport(getConfiguration());
		exportor.create(true, true);
	}

	/**
	 * Create database structure<br/>
	 * 
	 * @throws IOException
	 * @throws HibernateException
	 * 
	 * @throws HibernateException
	 * @throws IOException
	 */
	public static void dropSchema() throws HibernateException, IOException {
		SchemaExport exportor = new SchemaExport(getConfiguration());
		exportor.drop(true, true);
	}

	/**
	 * Update database structure<br/>
	 * 
	 * @throws IOException
	 * @throws HibernateException
	 * 
	 * @throws HibernateException
	 * @throws IOException
	 */
	public static void updateSchema() throws HibernateException, IOException {
		SchemaUpdate se = new SchemaUpdate(getConfiguration());
		se.execute(true, true);
	}

	/**
	 * Read Properties
	 * 
	 * @return Properties
	 * @throws IOException
	 * @throws Exception
	 */
	public static Properties getProperties() throws IOException {
		if (properties == null) {
			InputStream is = Thread.currentThread().getContextClassLoader()
					.getResourceAsStream(CONFIG_PROPERTIES_LOCATION);
			assert (null != is);
			properties = new Properties();
			properties.load(is);
		}
		return properties;
	}

	public static SessionFactory getSessionFactory() throws HibernateException,
			IOException {
		if (DatabaseHelper.sessionFactory == null) {
			sessionFactory = getConfiguration().buildSessionFactory();
		}
		return sessionFactory;
	}

}
